package cn.wit.summit.chwan.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by tangbl on 2017/12/1 0001.
 */

public class StringUtils {
    public static boolean isNotEmpty(String str) {
        return !(str == null || str.equals(""));
    }

    public static boolean isEmpty(String str) {
        return str == null || str.equals("");
    }

    public static boolean isNumeric(String code) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(code).matches();
    }

    public static String getSafeString(String str) {
        return str == null ? "" : str;
    }

    public static String getNameNoExt(String path) {
        int start = path.lastIndexOf('/');
        int end = path.lastIndexOf('.');
        if (end == -1)
            end = path.length();
        if (start == -1)
            return path.substring(0, end);
        else
            return path.substring(start + 1, end);
    }

    public static boolean matchStringLength(String s, int min, int max) {
        int sLength = getStringLength(s);
        return sLength >= min && sLength <= max;
    }

    public static int getStringLength(String s) {
        if (StringUtils.isEmpty(s)) return 0;

        return s.length() + getChineseCount(s);
    }

    public static int getChineseCount(String str) {
        int count = 0;
        String regEx = "[\\u4e00-\\u9fa5]"; // unicode编码，判断是否为汉字
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        while (m.find()) {
            for (int i = 0; i <= m.groupCount(); i++) {
                count = count + 1;
            }
        }
        return count;
    }
}
